package com.zmn.plat.admin.controller.product.picture;

import javax.annotation.Resource;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.admin.controller.product.ServProductBaseController;
import com.zmn.plat.model.entity.product.ServProduct;
import com.zmn.plat.model.entity.product.ServProductPicture;
import com.zmn.plat.services.interfaces.product.ServProductPictureService;

/**
 * @author tanbiao
 * @description 代码公用，对相同的代码进行抽离
 * @since 2019/9/29 17:29
 * @modifier
 **/
public class ServProductPictureBaseController extends ServProductBaseController {
    private Logger logger = LoggerFactory.getLogger(ServProductPictureBaseController.class);
    @Resource
    ServProductPictureService servProductPictureService;

    /**
     * 功能描述 ：图片详情
     *
     * @param mav
     * @param productId
     * @return void
     * @author tanbiao
     * @since 2019/9/29
     * @modifier
     */
    protected void detail(ModelAndView mav, Integer productId) {
        ServProduct servProduct = productId > 0 ? servProductService.findByKey(productId) : new ServProduct();
        mav.addObject("servProductVO", servProduct);

        ServProductPicture servProductPicture = servProductPictureService.findByProductId(productId);
        if (servProductPicture == null) {
            servProductPicture = new ServProductPicture();
        }
        mav.addObject("servProductPicture", servProductPicture);
        mav.addObject("flag", 3);
    }

    /**
     * 功能描述 ：添加/修改图片
     *
     * @param servProductPicture
     * @return com.zmn.common.dto.ResultDTO
     * @author tanbiao
     * @since 2019/9/29
     * @modifier
     */
    protected ResponseDTO addEdit(ServProductPicture servProductPicture) {
        String lable = "";
        try {

            ServProductPicture result = servProductPictureService.findByProductId(servProductPicture.getProductId());
            if (result != null) {
                servProductPicture.setProductPictureId(result.getProductPictureId());
            }

            if (servProductPicture.getProductPictureId() == null) {
                lable = "新增产品图片";
                servProductPicture.setCreater(getMcStaff().getRealName());
                servProductPicture.setCreateTime(DateUtil.getNow());
                servProductPicture.setUpdater(getMcStaff().getRealName());
                servProductPicture.setUpdateTime(DateUtil.getNow());
                servProductPictureService.insert(servProductPicture);

                return ResponseDTO.success(GlobalConsts.YES, "新增产品图片成功！");
            } else {
                lable = "修改产品图片";
                servProductPicture.setUpdater(getMcStaff().getRealName());
                servProductPicture.setUpdateTime(DateUtil.getNow());
                servProductPictureService.updateByKey(servProductPicture);
                return ResponseDTO.success(GlobalConsts.YES,"修改产品成功！");
            }
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改产品图片失败,servProductPicture:[%s],ex:[%s]", ex.getMessage(), ex));
            return ResponseDTO.fail(lable + "失败！");
        }
    }

}
